package com.example.controller;
//姓名密码登录--加令牌验证
import com.example.pojo.Emp;
import com.example.pojo.Result;
import com.example.service.EmpService;
import com.example.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){ //json
        log.info("传入的姓名和密码：{}",emp);
        Emp e = empService.login(emp);

        //登录成功 生成令牌 下发令牌
        if(e != null){
            Map<String, Object> map = new HashMap<>();
            map.put("id",e.getId());
            map.put("name",e.getName());
            map.put("password",e.getPassword());
            //将员工的信息封装到Map集合 到令牌里面 返回去
            String jwt = JwtUtils.generateJwt(map);

            return Result.success(jwt);
        }
        //登录失败 返回错误信息
        return Result.error( "用户名或者密码错误");
    }
}
